//
//  ZXKFTVewController.m
//  Rong360
//
//  Created by tianliwei on 1/3/14.
//  Copyright (c) 2014 tianliwei. All rights reserved.
//

#import "ZXKFTVewController.h"
#import "FCJGCell.h"
#import "WebViewController.h"
typedef enum {
    Sina,//新浪乐居
    Soufang//搜房网
}HouseUnionType;

@interface ZXKFTVewController ()
@property (nonatomic, assign)HouseUnionType houseType;
@property (nonatomic, strong)NSArray *sinaItems;
@property (nonatomic, strong)NSArray *sinaUrls;
@property (nonatomic, strong)NSArray *soufangItems;
@property (nonatomic, strong)NSArray *soufangUrls;
@property (nonatomic, strong) UISegmentedControl *segmentedControl;
@end

static NSString *CellIdentifier = @"Cell";

@implementation ZXKFTVewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title = @"最新看房团";
    [self initSegmentControl];
    self.houseType = Sina;
    [self.tableView registerNib:[UINib nibWithNibName:@"FCJGCell" bundle:nil] forCellReuseIdentifier:CellIdentifier];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.rowHeight = 44;

    self.sinaItems = @[@"北京",@"上海",@"广州",@"深圳",@"天津",@"沈阳",@"重庆",@"武汉"];
    self.soufangItems = @[@"北京",@"上海",@"广州",@"深圳",@"武汉",@"重庆",@"成都",@"长沙"];
    self.sinaUrls = @[@"bj",@"sh",@"gz",@"sz",@"tj",@"sy",@"cq",@"wh"];
    self.soufangUrls = @[@"bj.htm",@"sh.htm",@"gz.htm",@"sz.htm",@"wh.htm",@"cq.htm",@"cd.htm",@"cs.htm"];
}

- (void)initSegmentControl{
    
    NSArray *segmentedArray = [[NSArray alloc]initWithObjects:@"新浪乐居",@"搜房网",nil];
    
    //初始化UISegmentedControl
    
    self.segmentedControl = [[UISegmentedControl alloc]initWithItems:segmentedArray];
    
    self.segmentedControl.frame = CGRectMake(20.0, 20.0, 280.0, 35.0);
    
    self.segmentedControl.selectedSegmentIndex = 0;//设置默认选择项索引
    
    self.segmentedControl.tintColor = RGBCOLOR(0, 172, 238);
    
    //有基本四种样式
    self.segmentedControl.segmentedControlStyle = UISegmentedControlStylePlain;//设置样式
    
    [self.segmentedControl addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged];  //添加委托方法
    [self.view addSubview:self.segmentedControl];
}


//具体委托方法实例

-(void)segmentAction:(UISegmentedControl *)Seg{
    NSInteger Index = Seg.selectedSegmentIndex;
    switch (Index) {
        case 0:
            self.houseType = Sina;
            break;
        case 1:
            self.houseType = Soufang;
            break;
        default:
            break;
    }
    [self.tableView reloadData];
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    FCJGCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (self.houseType == Sina) {
        cell.mainLabel.text = [self.sinaItems objectAtIndex:indexPath.row];
    }else if (self.houseType == Soufang){
        cell.mainLabel.text = [self.soufangItems objectAtIndex:indexPath.row];
    }
    return cell;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (self.houseType == Sina) {
        return self.sinaItems.count;
    }else if (self.houseType == Soufang){
        return self.soufangItems.count;
    }
    return 0;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSString *url = nil;
    if (self.houseType == Sina) {        
        url = [NSString stringWithFormat:@"http://house.sina.cn/touch/index/?site=touch&ctl=lookhouse&act=city_line&city=%@",self.sinaUrls[indexPath.row]];
        self.hidesBottomBarWhenPushed = YES;
        WebViewController *webVC = [[WebViewController alloc]initWithUrl:url title:@"新浪乐居"];
        [self.navigationController pushViewController:webVC animated:YES];
    }else if (self.houseType == Soufang){
        url = [NSString stringWithFormat:@"http://m.soufun.com/kanfangtuan/%@",self.soufangUrls[indexPath.row]];
        self.hidesBottomBarWhenPushed = YES;
        WebViewController *webVC = [[WebViewController alloc]initWithUrl:url title:@"搜房网"];
        [self.navigationController pushViewController:webVC animated:YES];
    }
    
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
